//
//  addSceneContentView.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/6/19.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "addSceneContentView.h"

@interface addSceneContentView () <UIPickerViewDataSource, UIPickerViewDelegate>
{
    NSDictionary *jsonDic;
    NSUInteger picker_tag;
    NSArray *floorArray;
    NSArray *roomArray;
    NSArray *deviceArray;
    NSUInteger nowFloor, nowRoom, nowDevice, nowFundation;
    NSDictionary *fundationList;
}
@property (nonatomic, strong) UILabel *floorLabel;
@property (nonatomic, strong) UILabel *roomLabel;
@property (nonatomic, strong) UILabel *deviceLabel;
@property (nonatomic, strong) UILabel *fundationLabel;
@property (nonatomic, strong) UIView *pickerBackView;
@property (nonatomic, strong) UIPickerView *myPickerView;
@property (nonatomic, strong) UIButton *saveButton;

@end

@implementation addSceneContentView

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame andJsonDic:(NSDictionary *)dic
{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = BACKGROUND_COLOR;
        jsonDic = dic;
        picker_tag = 1;
        nowFloor = nowRoom = nowDevice = 0;
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *plistPath = [bundle pathForResource:@"FuctionList" ofType:@"plist"];
        fundationList = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
        
        [self addLabel];
        [self analyzeJsonDic];
        self.floorLabel.text = [[floorArray objectAtIndex:nowFloor] objectForKey:@"floorName"];
        self.roomLabel.text = [[roomArray objectAtIndex:nowRoom] objectForKey:@"roomName"];
        self.deviceLabel.text = [[deviceArray objectAtIndex:nowDevice] objectForKey:@"deviceName"];
        if(nowFundation == 0){
            self.fundationLabel.text = @"开";
        }else{
            self.fundationLabel.text = @"关";
        }
    }
    return self;
}

#pragma mark - UIPickerViewDelegate UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(picker_tag == 1){
        return [floorArray count];
    }else if(picker_tag == 2){
        return [roomArray count];
    }else if(picker_tag == 3){
        return [deviceArray count];
    }else if(picker_tag == 4){
        return 2;
    }
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    if(picker_tag == 1){
        return [[floorArray objectAtIndex:row] objectForKey:@"floorName"];
    }else if(picker_tag == 2){
        return [[roomArray objectAtIndex:row] objectForKey:@"roomName"];
    }else if(picker_tag == 3){
        return [[deviceArray objectAtIndex:row] objectForKey:@"deviceName"];
    }else if(picker_tag == 4){
        if(row == 0){
            return @"开";
        }else{
            return @"关";
        }
    }
    return @"";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(picker_tag == 1){
        nowFloor = row;
    }else if(picker_tag == 2){
        nowRoom = row;
    }else if(picker_tag == 3){
        nowDevice = row;
    }else if(picker_tag == 4){
        nowFundation = row;
    }
}

#pragma mark - method
- (void)addLabel
{
    [self addSubview:self.floorLabel];
    [self addSubview:self.roomLabel];
    [self addSubview:self.deviceLabel];
    [self addSubview:self.fundationLabel];
    [self addSubview:self.saveButton];
}

- (void)addPicker
{
    [self addSubview:self.pickerBackView];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.pickerBackView.bounds.size.width - 50, 5, 45, 25)];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
    [self.pickerBackView addSubview:button];
    [self.pickerBackView addSubview:self.myPickerView];
    [self.myPickerView selectRow:0 inComponent:0 animated:NO];
}

- (void)analyzeJsonDic
{
    floorArray = [jsonDic objectForKey:@"FLOOR"];
    NSDictionary *floor_dic = [floorArray objectAtIndex:nowFloor];
    NSString *floor_name = [floor_dic objectForKey:@"floorName"];
    
    NSArray *allRoom = [jsonDic objectForKey:@"ROOM"];
    NSMutableArray *rooms = [[NSMutableArray alloc] init];
    for (NSDictionary *dic in allRoom) {
        if([[dic objectForKey:@"floorName"] isEqualToString:floor_name]){
            [rooms addObject:dic];
        }
    }
    roomArray = rooms;
    NSDictionary *room_dic = [roomArray objectAtIndex:nowRoom];
    NSString *room_id = [room_dic objectForKey:@"roomId"];
    
    NSArray *allDevice = [jsonDic objectForKey:@"DEVICE"];
    NSMutableArray *devices = [[NSMutableArray alloc] init];
    for (NSDictionary *dic in allDevice) {
        if([[dic objectForKey:@"roomId"] integerValue] == [room_id integerValue]){
            [devices addObject:dic];
        }
    }
    deviceArray = devices;
}

- (NSDictionary *)getResult
{
    NSDictionary *device_dic = [deviceArray objectAtIndex:nowDevice];
    NSString *name = [device_dic objectForKey:@"deviceName"];
    NSString *type = [device_dic objectForKey:@"deviceType"];
    NSString *fundationName;
    if(nowFundation == 0){
        if([type isEqualToString:AIR_TYPE]){
            fundationName = AIR_ON;
        }else if([type isEqualToString:TV_TYPE]){
            fundationName = TV_ON;
        }else if([type isEqualToString:LIGHT_TYPE2]){
            fundationName = PD_ON;
        }else if([type isEqualToString:LIGHT_TYPE3]){
            fundationName = TD_VALUE4;
        }else if([type isEqualToString:CURTAIN_TYPE]){
            fundationName = CL_ON;
        }else if([type isEqualToString:MB_TYPE]){
            fundationName = MB_ON;
        }else if([type isEqualToString:STB_TYPE]){
            fundationName = STB_ON;
        }else if([type isEqualToString:DVD_TYPE]){
            fundationName = DVD_ON;
        }else if([type isEqualToString:AMP_TYPE]){
            fundationName = AMP_ON;
        }
    }else{
        if([type isEqualToString:AIR_TYPE]){
            fundationName = AIR_OFF;
        }else if([type isEqualToString:TV_TYPE]){
            fundationName = TV_OFF;
        }else if([type isEqualToString:LIGHT_TYPE2]){
            fundationName = PD_OFF;
        }else if([type isEqualToString:LIGHT_TYPE3]){
            fundationName = TD_VALUE0;
        }else if([type isEqualToString:CURTAIN_TYPE]){
            fundationName = CL_OFF;
        }else if([type isEqualToString:MB_TYPE]){
            fundationName = MB_OFF;
        }else if([type isEqualToString:STB_TYPE]){
            fundationName = STB_OFF;
        }else if([type isEqualToString:DVD_TYPE]){
            fundationName = DVD_OFF;
        }else if([type isEqualToString:AMP_TYPE]){
            fundationName = AMP_OFF;
        }
    }
    NSUInteger device_id = [[device_dic objectForKey:@"deviceId"] intValue];
    NSDictionary *deviceFuc = [fundationList objectForKey:type];
    NSDictionary *fucNameAndType = [deviceFuc objectForKey:fundationName];
    NSString *key_name = [fucNameAndType objectForKey:@"name"];
    NSString *key_type = [fucNameAndType objectForKey:@"type"];
    NSArray *device_keys = [jsonDic objectForKey:@"DEVICE_KEY"];
    NSString *sensorId;
    NSString *message;
    for (NSDictionary *d in device_keys) {
        if([[d objectForKey:@"deviceId"] intValue] == device_id
           && [[d objectForKey:@"keyName"] isEqualToString:key_name]
           && [[d objectForKey:@"keyType"] isEqualToString:key_type]){
            sensorId = [d objectForKey:@"sensorId"];
            if(sensorId != nil){
                NSString *sensorTable = [d objectForKey:@"sensorTable"];
                NSLog(@"sensorTable:%@",sensorTable);
                NSArray *sensorArray = [jsonDic objectForKey:sensorTable];
                for (NSDictionary *dic in sensorArray) {
                    if([[dic objectForKey:@"sensorId"] isEqualToString:sensorId]){
                        NSString *codeType = [dic objectForKey:@"codeType"];
                        NSString *freqType = [dic objectForKey:@"freqType"];
                        NSUInteger length = sensorId.length;
                        int x = [[sensorId substringWithRange:NSMakeRange(2, length - 2)] intValue];   //读取RA后面的数字
                        message = [NSString stringWithFormat:@"PT%@_%@-SEND-%@",codeType,freqType, [NSString stringWithFormat:@"%d",x]];
                    }
                }
            }else{
                message = nil;
            }
        }
    }
    if(nowFundation == 0){
        fundationName = @"开";
    }else{
        fundationName = @"关";
    }
    //NSLog(@"%@,%@,%@",name, type, message);
    NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:name, @"name", type, @"type", message, @"message", fundationName, @"fundationName", nil];
    return result;
}

#pragma mark - response
- (void)showPickerView1
{
    picker_tag = 1;
    [self.myPickerView reloadAllComponents];
    [self addPicker];
}

- (void)showPickerView2
{
    picker_tag = 2;
    [self.myPickerView reloadAllComponents];
    [self addPicker];
}

- (void)showPickerView3
{
    picker_tag = 3;
    [self.myPickerView reloadAllComponents];
    [self addPicker];
}

- (void)showPickerView4
{
    picker_tag = 4;
    [self.myPickerView reloadAllComponents];
    [self addPicker];
}

- (void)clickButton
{
    [self.pickerBackView removeFromSuperview];
    [self analyzeJsonDic];
    self.floorLabel.text = [[floorArray objectAtIndex:nowFloor] objectForKey:@"floorName"];
    self.roomLabel.text = [[roomArray objectAtIndex:nowRoom] objectForKey:@"roomName"];
    self.deviceLabel.text = [[deviceArray objectAtIndex:nowDevice] objectForKey:@"deviceName"];
    if(nowFundation == 0){
        self.fundationLabel.text = @"开";
    }else{
        self.fundationLabel.text = @"关";
    }
}

- (void)clickSaveButton
{
    [self clickButton];
    NSDictionary *result = [self getResult];
    self.getMyResult(result);
}

#pragma mark - getter setter
- (UILabel *)floorLabel
{
    if(!_floorLabel){
        _floorLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 30, (self.bounds.size.width - 15) / 2, 30)];
        _floorLabel.backgroundColor = [UIColor whiteColor];
        _floorLabel.textAlignment = NSTextAlignmentCenter;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPickerView1)];
        [_floorLabel addGestureRecognizer:tap];
        _floorLabel.userInteractionEnabled = YES;
    }
    return _floorLabel;
}

- (UILabel *)roomLabel
{
    if(!_roomLabel){
        _roomLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.bounds.size.width - 15) / 2 + 10, 30, (self.bounds.size.width - 15) / 2, 30)];
        _roomLabel.backgroundColor = [UIColor whiteColor];
        _roomLabel.textAlignment = NSTextAlignmentCenter;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPickerView2)];
        [_roomLabel addGestureRecognizer:tap];
        _roomLabel.userInteractionEnabled = YES;
    }
    return _roomLabel;
}

- (UILabel *)deviceLabel
{
    if(!_deviceLabel){
        _deviceLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 70, (self.bounds.size.width - 15) / 2, 30)];
        _deviceLabel.backgroundColor = [UIColor whiteColor];
        _deviceLabel.textAlignment = NSTextAlignmentCenter;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPickerView3)];
        [_deviceLabel addGestureRecognizer:tap];
        _deviceLabel.userInteractionEnabled = YES;
    }
    return _deviceLabel;
}

- (UILabel *)fundationLabel
{
    if(!_fundationLabel){
        _fundationLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.bounds.size.width - 15) / 2 + 10, 70, (self.bounds.size.width - 15) / 2, 30)];
        _fundationLabel.backgroundColor = [UIColor whiteColor];
        _fundationLabel.textAlignment = NSTextAlignmentCenter;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPickerView4)];
        [_fundationLabel addGestureRecognizer:tap];
        _fundationLabel.userInteractionEnabled = YES;
    }
    return _fundationLabel;
}

- (UIView *)pickerBackView
{
    if(!_pickerBackView){
        CGFloat height = self.myPickerView.bounds.size.height + 35;
        _pickerBackView = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - height, self.bounds.size.width, height)];
        _pickerBackView.backgroundColor = [UIColor whiteColor];
    }
    return _pickerBackView;
}

- (UIPickerView *)myPickerView
{
    if(!_myPickerView){
        _myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 35, 0, 0)];
        _myPickerView.backgroundColor = [UIColor lightGrayColor];
        _myPickerView.delegate = self;
        _myPickerView.dataSource = self;
    }
    return _myPickerView;
}

- (UIButton *)saveButton
{
    if(!_saveButton){
        _saveButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 115, self.bounds.size.width - 60, 35)];
        _saveButton.backgroundColor = TEXT_GREEN_COLOR;
        [_saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [_saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_saveButton addTarget:self action:@selector(clickSaveButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveButton;
}

@end
